package com.xhsj.user.sms.purchase.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.xhsj.user.hr.system.entity.TbGlobalUser;
import com.xhsj.user.hr.system.mapper.TbGlobalUserMapper;
import com.xhsj.user.hr.system.service.TbGlobalUserService;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseOrderSubtask;
import com.xhsj.user.sms.purchase.entity.TbPurchaseSubtaskList;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseOrderSubtaskMapper;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseSubtaskListMapper;
import com.xhsj.user.sms.purchase.vo.MyFindOrderSuntaskListVo;
import com.xhsj.user.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 采购订单子任务 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-06-08
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseOrderSubtask")
public class TbPurchaseOrderSubtaskService extends BaseService<TbPurchaseOrderSubtaskMapper, TbPurchaseOrderSubtask>{
    @Autowired
    private TbPurchaseOrderSubtaskMapper dao;

    @Autowired
    private TbPurchaseSubtaskListMapper tbPurchaseSubtaskListMapper;

    @Autowired
    private TbGlobalUserService tbGlobalUserService;

    @Autowired
    private TbGlobalUserMapper tbGlobalUserMapper;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderSubtask", allEntries=true)
    public boolean save(TbPurchaseOrderSubtask entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseOrderSubtask getById(Serializable id) {
        TbPurchaseOrderSubtask tbPurchaseOrderSubtask = super.getById(id);
        if (tbPurchaseOrderSubtask !=null) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("data_id",id);
            List<TbPurchaseSubtaskList> subtaskLists = tbPurchaseSubtaskListMapper.selectByMap(map);
            tbPurchaseOrderSubtask.setPurchaseOrderGoodsList(subtaskLists);
        }


        return tbPurchaseOrderSubtask;
    }

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderSubtask", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseOrderSubtask> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseOrderSubtask entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseOrderSubtask.class);
        Page<TbPurchaseOrderSubtask> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPurchaseOrderSubtask> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbPurchaseOrderSubtask",sync = true)
    public List<TbPurchaseOrderSubtask> selectAll() {
        return super.selectAll();
    }


    public TbPurchaseOrderSubtask getPurchaseOrderSubtaskById(String purchaseOrderId) {
        return dao.getPurchaseOrderSubtaskById(purchaseOrderId);
    }

    //分页查询我的采购订单子任务list
    public IPage<MyFindOrderSuntaskListVo> myFindOrderSuntaskList(Map<String, Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }
        Object subtaskNumber = data.get("subtaskNumber");
        Object supplierName = data.get("supplierName");
        Object purchaseCycle = data.get("purchaseCycle");
        Object userId = data.get("userId");
        IPage<TbPurchaseOrderSubtask> page = new Page(currentPage,pageSize);
        IPage<MyFindOrderSuntaskListVo> iPage = dao.myFindOrderSuntaskList(page, userId, subtaskNumber, supplierName, purchaseCycle);
        List<MyFindOrderSuntaskListVo> records = iPage.getRecords();
        for (MyFindOrderSuntaskListVo record : records) {

            String userId1 = record.getUserId();
            if (StringUtils.isNotBlank(userId1)) {
                TbGlobalUser user = tbGlobalUserService.getById(userId1);
                record.setAuditor(user.getFullName());
            }

            String roleId = record.getRoleId();
            if (StringUtils.isNotBlank(roleId)) {
                List<TbGlobalUser> list = tbGlobalUserMapper.getUserListByRoleId(roleId);
                String collect = list.stream().map(tbGlobalUser -> tbGlobalUser.getFullName()).collect(Collectors.joining(","));
                record.setAuditor(collect);
            }

            String deptId = record.getDeptId();
            if (StringUtils.isNotBlank(deptId)) {
                List<TbGlobalUser> list = tbGlobalUserMapper.getUserListByDeptId(deptId);
                String collect = list.stream().map(tbGlobalUser -> tbGlobalUser.getFullName()).collect(Collectors.joining(","));
                record.setAuditor(collect);
            }
        }


        return iPage;
    }
}
